package view.content;

import java.awt.Color;
import java.awt.Font;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextArea;

import org.json.simple.JSONObject;

public class LabelFormat {
	private Color	fontColor;
	private Font	font;
	private Color	backgroundColor;
	private String prefix;
	private String postfix;
	public LabelFormat(JSONObject format) {
		prefix = "";
		postfix ="";
		this.format(format);
	}

	public void format(JSONObject obj) {
		if(obj == null)
		{
			this.font = new Font("Arial", 0, 14);
			this.fontColor = this.getColor("000000");
		}
		else
		{
			String fontName = obj.get("font") == null ? "Arial" : obj.get("font").toString();
			String fontSize = obj.get("font-size") == null ? "14" : obj.get("font-size").toString();
			String fontColor = obj.get("color") == null ? "000000" : obj.get("color").toString();
			this.prefix = obj.get("prefix") == null ? "" : obj.get("prefix").toString();
			this.postfix = obj.get("postfix") == null ? "" : obj.get("postfix").toString();
			int style = 0;
			if (obj.get("font-style") != null) {
				style = Font.ITALIC | style;
			}
			if (obj.get("font-weight") != null) {
				style = Font.BOLD | style;
			}
	
			this.font = new Font(fontName, style, Integer.parseInt(fontSize));
			this.fontColor = this.getColor(fontColor);
		}
		// this.backgroundColor = this.getColor("background-color");
	}

	private Color getColor(String colorString) {
		Color color = new Color(Integer.parseInt(colorString.substring(0, 2), 16), Integer.parseInt(
				colorString.substring(2, 4), 16), Integer.parseInt(colorString.substring(4, 6), 16));

		return color;

	}

	public void applyStyle(JComponent comp) {
		if (this.font != null) {
			comp.setFont(this.font);
		}
		if (this.fontColor != null) {
			comp.setForeground(this.fontColor);
		}
		if (this.backgroundColor != null) {
			comp.setBackground(this.backgroundColor);
		}
		
		if(comp instanceof JLabel && comp!=null) {
			JLabel label = (JLabel) comp;
			label.setText(this.prefix + label.getText() + this.postfix);
		} else if (comp instanceof JTextArea && comp!=null) {
			JTextArea label = (JTextArea) comp;
			label.setText(this.prefix + label.getText() + this.postfix);
		} else {
		}
	}
}
